import {
    createStore
} from 'vuex'

import createPersistedState from "vuex-persistedstate";  //安装cnpm install --save vuex-persistedstate
import { getMenulist } from "@/http/api.js"
import { getToken, setToken, removeToken } from '@/utils/auth';
import {constantRoutes,asyncRoutes} from '@/router'
export default createStore({
    state: {
        userInfo:null,
        menuList: [],
        arr:[]
    },
    mutations: {
        // 存
        setToken(state, data) {
            state.userInfo = data
            console.log(state.userInfo);
            setToken(data)
        },

        setSignOut(state) {
            state.userInfo = null
            state.menuList = []
            state.buttonList = []
        },

        setMenuList(state, data) {
            
                state.menuList = data
        
            // console.log(state.menuList,32);
        },
        newMenus(state,data){
          state.menuList=[...constantRoutes,...data]
          console.log(state.menuList,11111);
          state.arr=data
        }
    },
    getters: {
        getMenuList(state) {
            return state.menuList
        }
    },
    actions: {
        // token
        userInfos({
            commit
        }, data) {
            commit("setToken", data)
        },

        signOuts({
            commit
        }) {
            commit("setSignOut")
        },
        // 筛选 动态路由是否与数据相符合  
         async filterRouter({commit},menus){
            // 新数组
            let arr=[]
            // 后台接受的数据循环遍历    asyncRoutes动态路由
            menus.forEach(key=>{
                arr.push(...asyncRoutes.filter(item=>item.name==key))
                console.log(arr,"arr");
            })
            commit ('newMenus',arr)
            return arr
         },
        async getLists({
            commit
        }, data) {
            return await getMenulist()
            //请求左侧菜单接口
            // getMenulist({commit}).then(res => {
            //     console.log(res.data,599);
                // commit("setMenuList", res)
            //     return res
            // })
        },
    },
    modules: {},
    plugins: [
        createPersistedState({
            storage: window.localStorage,
            key: "store",
            render(state) {
                return {
                    ...state
                };
            }
        })
    ]
})
